package com.二叉树2;


import sun.reflect.generics.tree.Tree;

import java.util.*;

public class TreeNode {
   public  TreeNode left;
    public TreeNode right;
    public  int val;

    public TreeNode(int val,TreeNode left, TreeNode right) {
        this.left = left;
        this.right = right;
        this.val = val;
    }

    public TreeNode(int value) {
        this.val = value;
    }
    public TreeNode(int[] nums){
           this(nums[0]);
        TreeNode root = this;

        int curIndex = 1; //当前节点
        int nodeNumber = 2; //行长度
        Deque<TreeNode> deque  = new LinkedList<>();
        deque.offer(root);
        TreeNode curNode ; //当前树
        int remainingLength =  nums.length - curIndex; //剩余长度
        while (remainingLength>=0){

            for(curIndex=curIndex;curIndex<curIndex+nodeNumber;curIndex+=2){
                curNode = deque.poll(); //取出头节点
                if(curIndex>=nums.length){
                    return;
                }
                curNode.left = new TreeNode(nums[curIndex]);
                deque.offer(curNode.left);

                if(curIndex+1>=nums.length){
                    return;
                }
                curNode.right = new TreeNode(nums[curIndex+1]);
                deque.offer(curNode.right);
            }
            curIndex += nodeNumber;
            remainingLength -= nodeNumber;
            nodeNumber = deque.size() * 2;

        }


    }
    public TreeNode(Integer[] nums){
        this(nums[0]);
        TreeNode cur = this;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(cur);
        for (int i = 1; i < nums.length; ) {
            TreeNode tmp = queue.poll();
            if(tmp == null){
                continue;
            }
            Integer leftNum = nums[i];

            TreeNode left = null;
            if(leftNum != null){
                left = new TreeNode(nums[i]);
            }
            tmp.left = left;
            ++i;
            if (i >= nums.length) {
                break;
            }
            Integer rightNum = nums[i];
            TreeNode right = null;
            if(rightNum != null){
                right = new TreeNode(nums[i]);
            }
            tmp.right = right;
            queue.add(left);
            queue.add(right);
            i++;
        }

    }
    public static TreeNode add(int[] nums) {

        TreeNode root = new TreeNode(nums[0]);

        int curIndex = 1; //当前节点
        int nodeNumber = 2; //行长度
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        TreeNode curNode; //当前树
        int remainingLength = nums.length - curIndex; //剩余长度
        while (remainingLength > 0) {

            for (int index = curIndex; index < curIndex + nodeNumber; index += 2) {  //+=2 是因为是一个节点下有两个子节点
                curNode = deque.poll(); //取出头节点
                if (curIndex >= nums.length) {
                    return root;
                }
                curNode.left = new TreeNode(nums[index]);
                deque.offer(curNode.left);

                if (index + 1 >= nums.length) {
                    return root;
                }
                curNode.right = new TreeNode(nums[index + 1]);
                deque.offer(curNode.right);
            }
            curIndex += nodeNumber;
            remainingLength -= nodeNumber;
            nodeNumber = deque.size() * 2;

        }
        return root;
    }

    @Override
    public String toString() {

      TreeNode left = this.left;
      TreeNode right = this.right;

    return "";
    }


    public static void main(String[] args) {
        System.out.println("");
        System.out.println("\\");
        TreeNode treeNode = add(new int[]{1,2,3,4,5,6,7});
        TreeNode node2 = new TreeNode(new int[]{1,2,3,4,5,6,7});
        node2.toString();
    }



}
